package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.aliyun.service.PubService;
import com.huilian.iotbox.data.utils.IotBoxUtil;
import com.huilian.iotbox.data.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TestController
 *
 * @Author mineChen
 * @Date 2021/8/26 下午 08:01
 */
@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController extends BaseController {
    @Value("${huilian.iot.productKey:null}")
    private String productKey;

    @Value("${huilian.iot.iotInstanceId:#{null}}")
    private String iotInstanceId = null;
    @Autowired
    private PubService pubService;

    private int flag = 0;
    /*@GetMapping("/send")
    public ServerResponse upCoin(Integer num, String deviceNo) {
        final String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        final String s = PositiveDataFrameCMD.upCoin(flag, num, timestamp);
        String getTopic = String.format(IotConstant.GET_TOPIC, productKey, deviceNo);
        PubResponse pubResponse = MessageBrokerManager.pub(productKey, getTopic, Base64.encodeBase64String(s.getBytes()), 0, iotInstanceId);
        flag++;
        if (!ObjectUtils.isEmpty(pubResponse) && pubResponse.getSuccess()) {
            log.info("发送上分指令: {}", s);
            log.info("pub success?:" + pubResponse.getSuccess());
            return ServerResponse.createBySuccess();
        } else {
            log.error("pub fail! :" + pubResponse.getErrorMessage());
            return ServerResponse.createBySuccess();

        }

    }

    @GetMapping("/setOut")
    public ServerResponse query(Integer data,String deviceNo) {
        final String s = PositiveDataFrameCMD.setOutwardCoinLevel(IotBoxUtil.singleInt2Hex(data));
        String getTopic = String.format(IotConstant.GET_TOPIC, productKey, deviceNo);
        PubResponse pubResponse = MessageBrokerManager.pub(productKey, getTopic, Base64.encodeBase64String(s.getBytes()), 0, iotInstanceId);
        flag++;
        if (!ObjectUtils.isEmpty(pubResponse) && pubResponse.getSuccess()) {
            log.info("发送设置向外投币电平指令: {}", s);
            log.info("pub success?:" + pubResponse.getSuccess());
            return ServerResponse.createBySuccess();
        } else {
            log.error("pub fail! :" + pubResponse.getErrorMessage());
            return ServerResponse.createBySuccess();

        }
    }

    @GetMapping("/setPort")
    public ServerResponse setPort(String deviceNo) {
        // 10001
//        final String s = PositiveDataFrameCMD.setSerialPort(38400, 5,0,0, true);
        // 10002
        final String s = PositiveDataFrameCMD.setSerialPort(115200, 8,0,0, false);
        String getTopic = String.format(IotConstant.GET_TOPIC, productKey, deviceNo);
        PubResponse pubResponse = MessageBrokerManager.pub(productKey, getTopic, Base64.encodeBase64String(s.getBytes()), 0, iotInstanceId);
        flag++;
        if (!ObjectUtils.isEmpty(pubResponse) && pubResponse.getSuccess()) {
            log.info("发送设置通讯串口指令: {}", s);
            log.info("pub success?:" + pubResponse.getSuccess());
            return ServerResponse.createBySuccess();
        } else {
            log.error("pub fail! :" + pubResponse.getErrorMessage());
            return ServerResponse.createBySuccess();

        }
    }

    @GetMapping("/penetrate")
    public ServerResponse penetrate(String deviceNo) {
        final String s = PositiveDataFrameCMD.penetrate(PositiveDataFrameCMD.queryTerminalAccounts());
        ;
        String getTopic = String.format(IotConstant.GET_TOPIC, productKey, deviceNo);
        PubResponse pubResponse = MessageBrokerManager.pub(productKey, getTopic, Base64.encodeBase64String(s.getBytes()), 0, iotInstanceId);
        flag++;
        if (!ObjectUtils.isEmpty(pubResponse) && pubResponse.getSuccess()) {
            log.info("发送透传指令: {}", s);
            log.info("pub success?:" + pubResponse.getSuccess());
            return ServerResponse.createBySuccess();
        } else {
            log.error("pub fail! :" + pubResponse.getErrorMessage());
            return ServerResponse.createBySuccess();

        }
    }

    @GetMapping("/queryBox")
    public ServerResponse queryBox(String deviceNo) {
        final String s = PositiveDataFrameCMD.boxParamsQuery();
        pubService.pub(s, deviceNo, productKey);
        return ServerResponse.createBySuccess();
    }

    // 查询链接
    @GetMapping("/queryLink")
    public ServerResponse queryLink(String deviceNo) {

        //AA0B0101 0000000100000002 08 DD
        //AA0B0101000000010000000208DD
        //AA110026AA0B0101000000010000000208DD40DD


        // 10002
        final String s = "AA0B0101000000010000000208DD";
        String tcp = PositiveDataFrameCMD.penetrate(s);
        log.info("发送查询链接指令tcp:" + tcp);
        String getTopic = String.format(IotConstant.GET_TOPIC, productKey, deviceNo);
        PubResponse pubResponse = MessageBrokerManager.pub(productKey, getTopic, Base64.encodeBase64String(tcp.getBytes()), 0, iotInstanceId);
        if (!ObjectUtils.isEmpty(pubResponse) && pubResponse.getSuccess()) {
            log.info("发送查询链接指令: {}", s);
            log.info("pub success?:" + pubResponse.getSuccess());
            return ServerResponse.createBySuccess();
        } else {
            log.error("pub fail! :" + pubResponse.getErrorMessage());
            return ServerResponse.createBySuccess();

        }
    }
    String millis = String.valueOf(System.currentTimeMillis());
    String orderNo = millis.substring(millis.length() - 6);
    @GetMapping("/payApply")
    public ServerResponse payApply(String deviceNo) {
//        millis = String.valueOf(System.currentTimeMillis());
//        orderNo = millis.substring(millis.length() - 6);
        orderNo = RandomStringUtils.random(6, true, true);
        log.info("orderNo: {}", orderNo);
        String command = PositiveDataFrameCMD.penetrate(NiuDanPositiveDataFrameCMD.eggPayQuery(orderNo));
        log.info("扭蛋机支付申请指令：{}", command);
        String getTopic = String.format(IotConstant.GET_TOPIC, productKey, deviceNo);
        final PubResponse pubResponse = MessageBrokerManager.pub(productKey, getTopic, Base64.encodeBase64String(command.getBytes()), 0,iotInstanceId);
        if (ObjectUtils.isEmpty(pubResponse)) {
            log.error("扭蛋机查询异常,请求失败");
            return ServerResponse.createByError();

        }
        if (!pubResponse.getSuccess()) {
            log.error("pub fail! :" + pubResponse.getErrorMessage());
        }
        return ServerResponse.createBySuccess();
    }

    @GetMapping("/upEgg")
    public ServerResponse upCoinEgg(String deviceNo) {
        String command = PositiveDataFrameCMD.penetrate(NiuDanPositiveDataFrameCMD.upCoinEgg(1, 1, orderNo));
//        String command = PositiveDataFrameCMD.penetrate(PositiveDataFrameCMD.eggPayQuery(orderNo));
        log.info("扭蛋机上分指令：{}", command);
        String getTopic = String.format(IotConstant.GET_TOPIC, productKey, deviceNo);
        final PubResponse pubResponse = MessageBrokerManager.pub(productKey, getTopic, Base64.encodeBase64String(command.getBytes()), 0,iotInstanceId);
        if (ObjectUtils.isEmpty(pubResponse)) {
            log.error("扭蛋机上分异常,请求失败");
            return ServerResponse.createByError();

        }
        if (!pubResponse.getSuccess()) {
            log.error("pub fail! :" + pubResponse.getErrorMessage());
        }
        return ServerResponse.createBySuccess();
    }


    @GetMapping("/setPulse")
    public ServerResponse setPulse(String deviceNo) {
        //AA1300303C1900190000190019000000000000001FDD

        String command = PositiveDataFrameCMD.setPulse(60,50,50, 1,50,50);
        log.info("设置脉冲指令：{}", command);
        String getTopic = String.format(IotConstant.GET_TOPIC, productKey, deviceNo);
        final PubResponse pubResponse = MessageBrokerManager.pub(productKey, getTopic, Base64.encodeBase64String(command.getBytes()), 0,iotInstanceId);
        if (ObjectUtils.isEmpty(pubResponse)) {
            log.error("设置脉冲指令异常,请求失败");
            return ServerResponse.createByError();

        }
        if (!pubResponse.getSuccess()) {
            log.error("pub fail! :" + pubResponse.getErrorMessage());
        }
        return ServerResponse.createBySuccess();
    }

    //查询基础参数-串口
    @GetMapping("/queryBaseParams")
    public ServerResponse queryBaseParams(String deviceNo) {

        String tcp = PositiveDataFrameCMD.queryBaseParams();
        log.info("tcp:" + tcp);
        String penetrate = PositiveDataFrameCMD.penetrate(tcp);

        String getTopic = String.format(IotConstant.GET_TOPIC, productKey, deviceNo);
        PubResponse pubResponse = MessageBrokerManager.pub(productKey, getTopic, Base64.encodeBase64String(penetrate.getBytes()), 0,iotInstanceId);
        if (!ObjectUtils.isEmpty(pubResponse) && pubResponse.getSuccess()) {
            log.info("发送查询基础参数指令: {}", penetrate);
            log.info("pub success?:" + pubResponse.getSuccess());
            return ServerResponse.createBySuccess();
        } else {
            log.error("pub fail! :" + pubResponse.getErrorMessage());
            return ServerResponse.createBySuccess();

        }
    }*/

    public static void main(String[] args) {
        System.out.println(IotBoxUtil.checkcode_0007("0B01010000000100000002"));
//        StringBuilder stringBuilder = new StringBuilder(IotConstant.CONTENT_PREFIX);
//        StringBuilder dataPrefix = new StringBuilder("1E0003");
//        final String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
//        final StringBuffer orderNo = IotBoxUtil.dataPlus3(timestamp);
//        final StringBuilder dataAre = dataPrefix
//                .append(IotBoxUtil.singleInt2Hex(1))
//                .append(IotBoxUtil.reverseHex(IotBoxUtil.int2ByteHex(1)))
//                .append(IotBoxUtil.reverseHex(IotBoxUtil.int2ByteHex(10)))
//                .append(orderNo)
//                .append(IotBoxUtil.complement(8));
//
//        stringBuilder
//                .append(dataAre)
//                .append(IotBoxUtil.checkcode_0007(dataAre.toString()))
//                .append(IotConstant.CONTENT_SUFFIX);
//        final String s = stringBuilder.toString().toUpperCase();
//        System.out.println(s);
    }
}
